import { get } from 'lodash'
import { getExtraUsers } from '@/components/storybook/utilties/chargebee'

export default (instance) => {
  let result = {}
  const { plan, addons, selectService, attendanceOnlyNumber, beforeServicePrice } = instance
  const bundleExtraUsersNumber = getExtraUsers(instance)
  result.planId = get(plan, 'pricing.otherId')
  if (addons && addons.length && Number(bundleExtraUsersNumber)) {
    result.addonsDtoList = []
     addons.map(i => {
      const tiers = get(i, 'pricing.tiers') || []
      if (i.otherId === 'Attendance-Dashboard-User-addon') {
        if (attendanceOnlyNumber > 0) {
          result.addonsDtoList.push({
            id: get(i, 'pricing.otherId'),
            unitPrice: get(i, 'pricing.price'),
            quantity: attendanceOnlyNumber,
          })
        }
      } else {
        result.addonsDtoList.push({
          id: get(i, 'pricing.otherId'),
          unitPrice: get(i, 'pricing.price'),
          quantity: bundleExtraUsersNumber,
          addonTiersDTOList: tiers.map(t => ({
            id: get(i, 'pricing.otherId'),
            startingUnit: t.starting_unit,
            endingUnit: t.ending_unit,
            price: t.price
          }))
        })
      }
    })
  }
  if (selectService) {
    result.chargesDTOS = [
      {
        id: get(selectService, 'pricing.otherId'),
        unitPrice: selectService.pricing.price > beforeServicePrice ? get(selectService, 'pricing.price') - beforeServicePrice : 0,
        servicePeriodDays: get(selectService, 'metadata.days'),
        chargeOnOption: 'on_event',
        chargeOnEvent: 'subscription_creation',
        quantity: 1
      }
    ]
  }

  return result
}
